package com.jingdianjichi.subject.domain.handler.subject;

import com.google.common.base.Preconditions;
import com.jingdianjichi.subject.common.enums.IsDeletedFlageEnum;
import com.jingdianjichi.subject.common.enums.SubjectInfoTypeEnum;
import com.jingdianjichi.subject.domain.convert.JudgeSubjectConverter;
import com.jingdianjichi.subject.domain.convert.MultipleSubjectConverter;
import com.jingdianjichi.subject.domain.entity.SubjectAnswerBo;
import com.jingdianjichi.subject.domain.entity.SubjectInfoBo;
import com.jingdianjichi.subject.domain.entity.SubjectOptionBo;
import com.jingdianjichi.subject.infra.basic.entity.SubjectJudge;
import com.jingdianjichi.subject.infra.basic.entity.SubjectMultiple;
import com.jingdianjichi.subject.infra.basic.service.SubjectJudgeService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 判断题目的策略类
 */
@Component
public class JudgeTypeHandler implements SubjectTypeHandler {
    @Resource
    private SubjectJudgeService subjectJudgeService;

    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.JUDGE;
    }

    @Override
    public void add(SubjectInfoBo subjectInfoBo) {
        //判断题目的插入
        Preconditions.checkNotNull(subjectInfoBo.getOptionList(), "答案选项为空");
        List<SubjectJudge> subjectJudgeList = new LinkedList<>();
        subjectInfoBo.getOptionList().forEach(option -> {
            // option 转化为SubjectJudge
            SubjectJudge subjectJudge = JudgeSubjectConverter.INSTANCE
                    .convertBoToEntity(option);
            //属性注入
            subjectJudge.setSubjectId(subjectInfoBo.getId());
            subjectJudge.setIsCorrect(option.getIsCorrect());
            subjectJudge.setIsDeleted(IsDeletedFlageEnum.UN_DELETE.getCode());
            subjectJudgeList.add(subjectJudge);
        });
        subjectJudgeService.batchInsert(subjectJudgeList);
    }

    @Override
    public SubjectOptionBo queue(Long subjectId) {
        SubjectJudge subjectJudge = new SubjectJudge();
        subjectJudge.setSubjectId(subjectId);
        subjectJudge.setIsDeleted(IsDeletedFlageEnum.UN_DELETE.getCode());
     List<SubjectJudge> JudgeResult=   subjectJudgeService.queryByCondition(subjectJudge);

        List<SubjectAnswerBo> subjectAnswerBos = JudgeSubjectConverter.INSTANCE
                .convertJudgeListToBoAnswerBoList(JudgeResult);
        SubjectOptionBo subjectOptionBo = new SubjectOptionBo();
        subjectOptionBo.setOptionList(subjectAnswerBos);
        return subjectOptionBo;
    }
}

